package com.lanou.musicbox.fragment.seekmusic;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.seekmusic.EnterTheSeekMusic_SingleAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.Single;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵宝龙 on 15/10/24.
 * 搜索_单曲页面
 */
public class EnterTheSeekMusic_SingleFragment extends BaseFragment {
    private ExpandableListView singleListView;
    private EnterTheSeekMusic_SingleAdapter adapter;
    private VolleySingleton volleySingleton;
    private List<Single.DataEntity> data;
    private String newUrl;


    // 接收传过来的str
    public static EnterTheSeekMusic_SingleFragment passData(Context mContext, String str) {
        EnterTheSeekMusic_SingleFragment fragment = new EnterTheSeekMusic_SingleFragment();
        Log.i("wangshimei", str + " 88888");
        Bundle bundle = new Bundle();
        bundle.putString("str", str);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.entertheseekmusic_single, null);
        singleListView = (ExpandableListView) view.findViewById(R.id.singleListView);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        singleListView.setCacheColorHint(1);
        Bundle bundle = getArguments();
        String url = bundle.getString("str");
        newUrl = Contant.Url.URL_Seek_single_first+ url +Contant.Url.URL_Seek_single_end;
        Log.i("lanou444484", "onCreateView "+newUrl);
        initData();
        return view;
    }

    private void initData() {
        data = new ArrayList<>();

        StringRequest request = new StringRequest(newUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Single s = gson.fromJson(response, Single.class);
                data = s.getData();
                adapter = new EnterTheSeekMusic_SingleAdapter(data, getActivity());
                singleListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }
}
